<template>
    <div class="">
        <div id="header" :class="{ 'fixed-header': isHeaderFixed }">
          <div id="header_nav">
            <div id="header_nav1">
                <img id="nav1-img" src="https://course.myhope365.com/img/logo-text-green.2137bfaa.png" alt="">
                <div id="nav_left">
                    <ul>
                        <li id="nav_index"
                         @click="GOtoIndex"
                        style="float: left;
                    list-style: none;
                    margin-left: 20px;
                    margin-top: 20px;"><a  href=""
                          style=" text-decoration: none;
                                color:black;
                          font-size: 19px;
                          font-weight: bold; "
                         
                          >首页</a></li>
                        <li class="headerdetails" style="float: left;
                    list-style: none;
                    margin-left: 20px;
                    margin-top: 20px;"><a href="" id="header-coursesort"
                          style=" text-decoration: none;
                       color:black;
                          font-size: 19px;
                          font-weight: bold;"
                          @mouseenter="toggleList(true)" @mouseleave="toggleList(false)">课程分类 ⌵</a>  
                          <div id="courseDetail" v-show="isShow"  @mouseenter="toggleList(true)" @mouseleave="toggleList(false)">
                             <ul>
                                 <li id="detail" v-for="item in courseDetail" :key="item.subjectId">
                                      <a href=""> {{ item.title }}</a>
                                </li>
                             </ul>
                          </div>                                      
                        </li>
                        <li id="nav-plat" style="float: left;
                    list-style: none;
                    margin-left: 20px;
                    margin-top: 20px;"><a href="https://stu.myhope365.com/home"
                          style=" text-decoration: none;
                           color:black;
                          font-size: 19px;
                          font-weight: bold;
                         ">教学管理云平台</a></li>
                    </ul>
                </div>
                <div id="search">
                    <input 
                        type="text" 
                        placeholder="搜索课程~"
                        v-model="searchKeyword"
                        @keyup.enter="handleSearch"
                    >
                    <button id="search-button" @click="handleSearch">
                        <img src="" alt="">
                    </button> 
                </div>
                 <div id="login">
                    <span><img @click="ToCart" src="" alt=""> </span>
                    <LoginView />
                </div>
            </div>
          </div>
        </div>
    </div>
</template>

<script>
import { getDetail } from "@/api/request";
import LoginView from "@/components/loginView.vue";

export default {
    name:'TopView',
    data() {
        return {
            isShow: false,
            courseDetail: [],
            isHeaderFixed: false,
            scrollPosition: 0,
            searchKeyword: ''
        }
    },
    components: {
        LoginView,
    },
    created() {
        getDetail({"enable": "1"}).then(res => {
            this.courseDetail = res.rows;
        });
        window.addEventListener('scroll', this.handleScroll);
    },
    beforeDestroy() {
        window.removeEventListener('scroll', this.handleScroll);
    },
    methods: {
        ToCart(){
      this.$router.push({ path: '/cart' }); 
    },
        GOtoIndex() {
            this.$router.push('/'); 
        },
        toggleList(visible) {
            this.isShow = visible;
        },
        handleScroll() {
            this.scrollPosition = window.scrollY || document.documentElement.scrollTop;
            this.isHeaderFixed = this.scrollPosition > 200;
        },
        handleSearch() {
    const keyword = this.searchKeyword.trim();
    const route = this.$router.resolve({
        path: '/search',
        query: { keyword }
    });
    window.open(route.href, '_blank');
},
      
    }
}
</script>

<style lang="scss" scoped>
#header {
    width: 100%;
    height: 80px;
    background-color: white;
    transition: all 0.3s ease;
    
    &.fixed-header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1000;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        animation: slideDown 0.3s ease-out;
    }

    #header_nav {
        height: 80px;

        #header_nav1 {
            display: flex;
            width: 100%;
            height: 80px;
            margin: auto;
            #nav1-img{
                width: 132;
                height: 40px;
                margin: auto 100px;
            }
            #nav_left {
                height: 100%;
            }
            #search {
                display: flex;
                margin-top: 20px;
                margin-left: 150px;
                position: relative;

                input {
                    width: 270px;
                    height: 40px;
                    border-radius: 15px 0 0 15px;
                    border: none;
                    outline: none;
                    border: 1px solid #d7d3d3;
                    padding: 0 15px;
                    font-size: 14px;
                }

                button {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    width: 40px;
                    height: 40px;
                    border-radius: 0 15px 15px 0;
                    border: 1px solid #fff;
                    background-color: rgb(0,178,120);
                    cursor: pointer;
                    img{
                        width: 20px;
                        height: 20px;
                       
                    }
                }
            }
        }
    }
}

@keyframes slideDown {
    from {
        transform: translateY(-100%);
    }
    to {
        transform: translateY(0);
    }
}

#login {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-left: 20px;

    span {
        background-color: #fff;
        border-radius: 50px;
        width: 36px;
        height: 36px;
        img{
            margin: 10px;
        }
    }
    #show-login-btn {
        width: 100px;
        height: 30px;
        background-color: #fff;
        margin-left: 20px;
        padding-left: 10px;
        border-radius: 15px;
        text-decoration: none;
        color: black;
        font-size: 15px;
        font-weight: bold;
        line-height: 30px;
        text-align: center;
        display: block;
        box-shadow: 10px 10px 10px rgba(0, 0, 0, 0.3);
        cursor: pointer;
    }
}
#nav_index {
    margin-left: 20px; 
}
.headerdetails{
    position: relative;
}
#courseDetail{
    background-color: white;
    width: 140px;
    border: 1px solid #c6cbcc;
    position:absolute;
    z-index: 999;
    margin-left: -20px;
    padding: 5px 0; 
    ul{
        padding:0;
    }
}

#detail {
    list-style: none;
    cursor: pointer;
    height: 30px;
   width: 100%;
    
    a {
    display: block; 
    padding: 5px 10px; 
    text-align: left; 
    line-height: 20px;
    font-size: 14px;
    text-decoration: none;
    color: #555;
    }
}
#detail:hover{
    background-color: #def1e6;
}
.search-input::placeholder {
  padding-left: 16px; 
}
</style>